package vn.com.vega.music.view.adapter;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.AlbumDetail.ISelectSongPlay;
import vn.com.vega.chacha.v2.Appinstant;
import vn.com.vega.music.database.DataStore;
import vn.com.vega.music.device.FileStorage;
import vn.com.vega.music.objects.Playlist;
import vn.com.vega.music.objects.Song;
import vn.com.vega.music.utils.Const;
import vn.com.vega.music.utils.Util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PlaylistDetailAdapter extends BaseAdapter {
	private static int PLATLIST_DETAIL_TYPE = 0;
	public static int HEADER_TYPE = 1;
	// private static int ROW_SONG_TYPE = 2;
	private static int NUMBER_TYPE = 2;

	private Context mContext;
	private LayoutInflater inflater;
	private Playlist mPlaylist;
	private DataStore mDataStore;
	private ISelectSongPlay selectSongPlayInterface;

	public void setSelectSongPlayInterface(ISelectSongPlay interfaceSelect) {
		selectSongPlayInterface = interfaceSelect;
	}

	public PlaylistDetailAdapter(Context context, Playlist playlist) {
		mContext = context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mPlaylist = playlist;
		mDataStore = DataStore.getInstance();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 2 /* + mPlaylist.songList.size() */;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		/*
		 * if (position >= 2) { return mPlaylist.songList.get(position); } else
		 * {
		 */
		return null;
		// }
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		int viewType = getItemViewType(position);
		PlaylistDetailHolder holder;
		if (viewType == PLATLIST_DETAIL_TYPE) {
			// if (convertView == null) {
			convertView = inflater.inflate(R.layout.album_detail_item_artist,
					null);
			holder = new PlaylistDetailHolder();
			holder.playlistThumb = (ImageView) convertView
					.findViewById(R.id.imv_album_thumb);
			holder.playlistName = (TextView) convertView
					.findViewById(R.id.tv_album_name);
			holder.playlistCreated = (TextView) convertView
					.findViewById(R.id.tv_album_artist);
			// holder.playlistDes = (TextView) convertView
			// .findViewById(R.id.tv_album_des);
			// convertView.setTag(holder);
			// } else {
			// holder = (PlaylistDetailHolder) convertView.getTag();
			// }
			Appinstant app = (Appinstant) mContext.getApplicationContext();
			holder.playlistName.setText(mPlaylist.title);
			if (mPlaylist.type == Playlist.TYPE_USER_CREATED
					|| mPlaylist.type == Playlist.TYPE_FAVORITE) {
				String userName = mDataStore.getConfig(Const.KEY_FULL_NAME);
				if (Util.checkStringIsemptyOrNull(userName)) {
					userName = mDataStore.getConfig(Const.KEY_PHONE_NUMBER);
				}
				holder.playlistCreated.setText(userName);
				FileStorage fileStorage = new FileStorage();
				String avatarPath = fileStorage.getAvatarCachePath();
				if (Util.checkStringIsemptyOrNull(avatarPath)) {
					Bitmap myBitmap = BitmapFactory.decodeFile(fileStorage
							.getAvatarCachePath());
					if (myBitmap != null) {
						holder.playlistThumb.setImageBitmap(myBitmap);
					} else {
						app.mImageLoader.download(mDataStore.getConfig(Const.KEY_AVATAR),
								holder.playlistThumb);
					}
				} else {
					app.mImageLoader.download(mDataStore.getConfig(Const.KEY_AVATAR),
							holder.playlistThumb);
				}
			} else {
				app.mImageLoader.download(mPlaylist.userThumb,
						holder.playlistThumb);
				holder.playlistCreated.setText(mPlaylist.userName);
			}

			// holder.playlistDes.setText(mPlaylist.description);
			return convertView;

		} else /* if (viewType == HEADER_TYPE) */{
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.header_list_song, null);
				holder = new PlaylistDetailHolder();
				holder.playAllSong = (TextView) convertView
						.findViewById(R.id.tv_play_all_song);
				holder.selectSongPlay = (TextView) convertView
						.findViewById(R.id.tv_select_song_play);
				holder.playAllSong.setOnClickListener(playAllClickListener);
				holder.selectSongPlay.setOnClickListener(selectSongPlayListener);
//				convertView.setTag(holder);
			} /*else {
				holder = (PlaylistDetailHolder) convertView.getTag();
			}*/
			return convertView;
		} /*
		 * else { if (convertView == null) { convertView =
		 * inflater.inflate(R.layout.song_item, null); holder = new
		 * PlaylistDetailHolder(); holder.songName = (TextView) convertView
		 * .findViewById(R.id.tv_song_title); holder.songListenCount =
		 * (TextView) convertView .findViewById(R.id.tv_count_song_listen);
		 * holder.songArtistName = (TextView) convertView
		 * .findViewById(R.id.tv_artist_name); convertView.setTag(holder); }
		 * else { holder = (PlaylistDetailHolder) convertView.getTag(); } Song
		 * song = (Song) mPlaylist.songList.get(position - ROW_SONG_TYPE);
		 * holder.songArtistName.setText(song.artist_name);
		 * holder.songName.setText(song.title);
		 * holder.songListenCount.setText(song.viewCount + ""); return
		 * convertView; }
		 */
	}

	View.OnClickListener playAllClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (selectSongPlayInterface != null) {
				selectSongPlayInterface.playAllSong();
			}
		}
	};

	View.OnClickListener selectSongPlayListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (selectSongPlayInterface != null) {
				selectSongPlayInterface.selectSongPlay();
			}
		}
	};

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		if (position == 0) {
			return PLATLIST_DETAIL_TYPE;
		} else /* if (position == 1) */{
			return HEADER_TYPE;
		} /*
		 * else return ROW_SONG_TYPE;
		 */
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return NUMBER_TYPE;
	}

	static class PlaylistDetailHolder {
		ImageView playlistThumb;
		// TextView playlistDes;
		TextView playlistName;
		TextView playlistCreated;
		TextView songArtistName;
		TextView songName;
		TextView songListenCount;
		TextView playAllSong;
		TextView selectSongPlay;
	}
}
